package com.goods.controller.business;

import com.github.pagehelper.PageInfo;
import com.goods.business.service.ProductService;
import com.goods.business.service.ProductStockService;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/9/24 16:56
 * @description：物资资料
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/business/product")
public class ProductWebController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductStockService productStockService;

    //http://www.localhost:8989/business/product/findProductList?pageNum=1&pageSize=6&name=&categoryId=&supplier=&status=0
    //物质资料 分页
    @GetMapping("findProductList")
    public ResponseBean findProductList(HttpServletRequest request){
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        String name = request.getParameter("name");
        String categoryId = request.getParameter("categoryId");
        String supplier = request.getParameter("supplier");
        String status = request.getParameter("status");
        String categorys = request.getParameter("categorys");

        PageInfo<Product> page = productService.findProductList(pageNum,pageSize,name,categoryId,supplier,status,categorys);
        Map<String, Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getList());
        return ResponseBean.success(data);
    }

    //物资资料 添加
    @PostMapping("add")
    public ResponseBean add(@RequestBody ProductVO productVO){
        productService.add(productVO);
        return ResponseBean.success();
    }

    //物资资料 修改之 查单个
    @GetMapping("edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        ProductVO productVo = productService.edit(id);
        return ResponseBean.success(productVo);
    }

    //物资资料 修改之 改单个
    @PutMapping("update/{id}")
    public ResponseBean update(@RequestBody ProductVO productVo){
        productService.update(productVo);
        return ResponseBean.success();
    }

    //物资资料 删除
    @DeleteMapping("delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        productService.delete(id);
        return ResponseBean.success();
    }

    //物资资料 回收站
    @PutMapping("remove/{id}")
    public ResponseBean remove(@PathVariable Long id){
        productService.remove(id);
        return ResponseBean.success();
    }

    //物资资料 还原
    @PutMapping("back/{id}")
    public ResponseBean back(@PathVariable Long id){
        productService.back(id);
        return ResponseBean.success();
    }

    //物资资料 通过审核
    @PutMapping("publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        productService.publish(id);
        return ResponseBean.success();
    }

    //入库记录 物资资料 分页查询
    //http://www.localhost:8989/business/product/findProducts?pageNum=1&pageSize=6&status=0&categorys=20,43,53&name=sss
    @GetMapping("findProducts")
    public ResponseBean findProducts(HttpServletRequest request){
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        String status = request.getParameter("status");
        String categorys = request.getParameter("categorys");
        String name = request.getParameter("name");
        PageInfo<Product> page = productService.findProductList(pageNum,pageSize,name,null,null,status,categorys);
        Map<String, Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getList());
        return ResponseBean.success(data);
    }

    //入库记录 物资库存 分页查询
    @GetMapping("findProductStocks")
    public ResponseBean findProductStocks(HttpServletRequest request){
        String pageSize = request.getParameter("pageSize");
        String pageNum = request.getParameter("pageNum");
        String categorys = request.getParameter("categorys");
        String name = request.getParameter("name");
        PageInfo<ProductStockVO> page = productStockService.findProductStocks(pageNum,pageSize,categorys,name);
        Map<String, Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getList());
        return ResponseBean.success(data);
    }

    //入库记录 物资库存 全部库存
    @GetMapping("findAllStocks")
    public ResponseBean findAllStocks(HttpServletRequest request){
        String pageSize = request.getParameter("pageSize");
        String pageNum = request.getParameter("pageNum");
        String categorys = request.getParameter("categorys");
        String name = request.getParameter("name");
        PageInfo<ProductStockVO> page = productStockService.findProductStocks(pageNum,pageSize,categorys,name);
        return ResponseBean.success(page.getList());
    }
}
